grErp.controller('SubproCtrl',['$scope','$filter','subProFactory',function($scope,$filter,subProFactory){
    $scope.items=[{number:0}];
    $scope.moreItem=function(){
        $scope.items.push({});
    }
    $scope.removeItem=function(index){
        if(confirm("确认删除")){
            $scope.items.splice(index,1);
        }
    }
    $scope.inAddInit=function(){
        subProFactory.ajaxGetSubPro().then(function(d){
            $scope.product=d;
        })
    }
    $scope.inpProductSelect=function(index){
        $scope.pro=$scope.product[index];
        console.log( $scope.pro);
    }
    $scope.incnt=function(){
        if($filter('isValidValue')($scope.pro.son_unit_cnt)){
            $scope.pro.number=parseInt($scope.pro.son_unit_cnt);
        }else{
            $scope.pro.number=0;
        }
        if($filter('isValidValue')($scope.pro.mother_unit_cnt)){
            $scope.pro.number+=$scope.pro.mother_unit_cnt*parseInt($scope.pro.mother_to_son);
        }
    }
    $scope.init=function(){
        subProFactory.ajaxGetSubproInventoryProductList().then(function(d){
            $scope.productList=d;
        })
    }
    $scope.productSelect=function(itemIndex,productIndex){
        $scope.items[itemIndex].product=$scope.productList[productIndex];
        $scope.items[itemIndex].number=0;
        $scope.items[itemIndex].mother_unit_cnt="";
        $scope.items[itemIndex].totalprice="";
        $scope.items[itemIndex].son_unit_cnt="";
    }
    $scope.cnt=function(itemIndex){
        if($filter('isValidValue')($scope.items[itemIndex].son_unit_cnt)){
            $scope.items[itemIndex].number=parseInt($scope.items[itemIndex].son_unit_cnt);
        }else{
            $scope.items[itemIndex].number=0;
        }
        if($filter('isValidValue')($scope.items[itemIndex].mother_unit_cnt)){
            $scope.items[itemIndex].number+=$scope.items[itemIndex].mother_unit_cnt*parseInt($scope.items[itemIndex].product.mother_to_son);
        }
        $scope.items[itemIndex].totalprice=$scope.items[itemIndex].number*parseFloat($scope.items[itemIndex].product.unitprice);
    }

}]);
